package com.dm.hdfxxt.projectmanage.controller;

import com.dm.hdfxxt.projectmanage.service.ProjectService;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "goProject")
public class ProjectController {
    private static final Logger logger = LoggerFactory.getLogger(ProjectController.class);

    @Autowired
    private ProjectService projectService;


    /**
         * @Description 修改用户密码
         * @Author zhangxue
         * @Date 8:48 2018/9/30
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/updatePassword",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> updatePassword(@RequestBody Map<String,Object> param){
        Map<String,Object> ResultMap = new HashMap<>();
        ResultMap = projectService.updatePassword(param);
        return ResultMap;
    }
    /**
         * @Description 查询左侧项目树
         * @Author zhangxue
         * @Date 11:03 2018/9/28
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/queryProjectTree",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> queryProjectTree(HttpServletRequest request){
        Map<String,Object> param = new HashMap<String, Object>();
        Map<String,Object> ResultMap = new HashMap<>();
        //查询所有一级项目
        List<Map<String,Object>> FirstLevelList = projectService.queryFirstLevel(param);
        List<Map<String,Object>> ResultList = new ArrayList<Map<String, Object>>();

        if(FirstLevelList!=null){
            for(int i=0;i<FirstLevelList.size();i++){
                Map<String,Object> ParamMap1 = FirstLevelList.get(i);
                List<Map<String,Object>> SecondLevelList = projectService.querySecondLevel(ParamMap1);
                for(int j=0;j<SecondLevelList.size();j++){
                    Map<String,Object> ParamMap2 = SecondLevelList.get(j);
                    List<Map<String,Object>> ThirdLevelList = projectService.querySecondLevel(ParamMap2);
                    SecondLevelList.get(j).put("children",ThirdLevelList);
                }
                FirstLevelList.get(i).put("children",SecondLevelList);
            }
        }else{
            ResultMap.put("Status",0);
            return ResultMap;
        }
        List<Map<String,Object>> SrList = new ArrayList<Map<String, Object>>();
        List<Map<String,Object>> ZcList = new ArrayList<Map<String, Object>>();
        for(int i=0;i<FirstLevelList.size();i++){
            String FirstLevelType = FirstLevelList.get(i).get("Type").toString();
            if("收入".equals(FirstLevelType)){
                SrList.add(FirstLevelList.get(i));
            }else if("支出".equals(FirstLevelType)){
                ZcList.add(FirstLevelList.get(i));
            }
        }
        Map<String,Object> SrMap = new HashMap<String, Object>();
        SrMap.put("title","收入");
        SrMap.put("expand","true");
        SrMap.put("children",SrList);
        Map<String,Object> ZcMap = new HashMap<String, Object>();
        ZcMap.put("title","支出");
        ZcMap.put("expand","true");
        ZcMap.put("children",ZcList);
        ResultList.add(SrMap);
        ResultList.add(ZcMap);
        ResultMap.put("Status",1);
        ResultMap.put("TreeJson",ResultList);
        return ResultMap;

    }

    /**
     * @Description 查询所有活动主题
     * @Author zhangxue
     * @Date 8:57 2018/9/28
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/queryTheme",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> queryTheme(){
        Map<String,Object> param = new HashMap<String, Object>();
        Map<String,Object> ResultMap = new HashMap<>();
        List<Map<String,Object>> ThemeList = projectService.queryTheme(param);
        ResultMap.put("ThemeList",ThemeList );
        return ResultMap;
    }

    /**
         * @Description 查询所有一级项目
         * @Author zhangxue
         * @Date 8:57 2018/9/28
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/queryFirstLevel",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> queryFirstLevel(){
        Map<String,Object> param = new HashMap<String, Object>();
        Map<String,Object> ResultMap = new HashMap<>();
        List<Map<String,Object>> FirstLevelList = projectService.queryFirstLevel(param);
        ResultMap.put("FirstLevel",FirstLevelList );
        return ResultMap;
    }

    /**
     * @Description 通过父级ID查询所有二级项目
     * @Author zhangxue
     * @Date 8:57 2018/9/28
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/querySecondLevel",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> querySecondLevel(@RequestBody Map<String,Object> param){
        Map<String,Object> ResultMap = new HashMap<>();
        List<Map<String,Object>> SecondLevelList = projectService.querySecondLevel(param);
        ResultMap.put("SecondLevel",SecondLevelList );
        return ResultMap;
    }

    /**
         * @Description 新增一级项目
         * @Author zhangxue
         * @Date 9:00 2018/9/28
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/saveFirstLevel",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> saveFirstLevel(@RequestBody Map<String,Object> param){
        Map<String,Object> ResultMap = new HashMap<>();
        ResultMap = projectService.saveFirstLevel(param);
        return ResultMap;
    }

    /**
         * @Description 新增二级项目
         * @Author zhangxue
         * @Date 9:07 2018/9/28
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/saveSecondLevel",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> saveSecondLevel(@RequestBody Map<String,Object> param){
        Map<String,Object> ResultMap = new HashMap<>();
        ResultMap = projectService.saveSecondLevel(param);
        return ResultMap;
    }

    /**
         * @Description 为最下级项目绑定部门
         * @Author zhangxue
         * @Date 17:22 2018/9/28
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/updateProjectBm",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> updateProjectBm(@RequestBody Map<String,Object> param){
        Map<String,Object> ResultMap = new HashMap<>();
        ResultMap = projectService.updateProjectBm(param);
        return ResultMap;
    }

    /**
         * @Description 根据部门查询所有需要录入信息
         * @Author zhangxue
         * @Date 9:05 2018/9/29
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/queryNeedInputInfo",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> queryNeedInputInfo(@RequestBody Map<String,Object> param){
        Map<String,Object> ResultMap = new HashMap<>();
        List<Map<String,Object>> ResultList = projectService.queryNeedInputInfo(param);
        ResultMap.put("ResultList",ResultList );
        return ResultMap;
    }

    /**
     * @Description 保存业务信息
     * @Author zhangxue
     * @Date 9:07 2018/9/28
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/saveServiceInfo",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> saveServiceInfo(@RequestBody Map<String,Object> param){
        Map<String,Object> ResultMap = new HashMap<>();
        ResultMap = projectService.saveServiceInfo(param);
        return ResultMap;
    }

    /**
         * @Description 查询所有已录入数据
         * @Author zhangxue
         * @Date 10:37 2018/9/29
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/queryAllServiceDataInfos",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> queryAllServiceDataInfos(@RequestBody Map<String,Object> param){
        Map<String,Object> ResultMap = new HashMap<>();
        List<Map<String,Object>> ResultList = projectService.queryAllServiceDataInfos(param);
        Integer total = 0;
        for(int i=0;i<ResultList.size();i++){
            String money = ResultList.get(i).get("PMoney").toString();
            total += Integer.valueOf(money);
        }
        ResultMap.put("ResultList",ResultList );
        Double SRTotal = 0.0;
        Double ZCTotal = 0.0;
        for(int i=0;i<ResultList.size();i++){
            String Type = ResultList.get(i).get("Type").toString();
            Double PMoney = Double.valueOf(ResultList.get(i).get("PMoney").toString());
            if("收入".equals(Type)){
                SRTotal += PMoney;
            }else if("支出".equals(Type)){
                ZCTotal += PMoney;
            }
        }
        Double ChaE = ZCTotal-SRTotal;
        param.put("CreateTime",param.get("EditDate"));
        List<Map<String,Object>> SaleResultList = projectService.queryDataTotal(param);

        int SaleTotalSum = 0;
        for(int i=0;i<SaleResultList.size();i++){
            int SaleTotal = Integer.valueOf(SaleResultList.get(i).get("SaleTotal").toString());
            SaleTotalSum += SaleTotal;
        }
        Double FXRate = ChaE/SaleTotalSum;
        FXRate = FXRate*100;
        String FXRateStr = String.format("%.2f", FXRate)+"%";
        ResultMap.put("TotalMoney",total );
        ResultMap.put("SRTotal",SRTotal );
        ResultMap.put("ZCTotal",ZCTotal );
        ResultMap.put("ChaE",ChaE );
        ResultMap.put("SaleTotalSum",SaleTotalSum );
        ResultMap.put("FXRateStr",FXRateStr );
        ResultMap.put("ResultList",ResultList );
        return ResultMap;
    }

    /**
         * @Description 保存礼品
         * @Author zhangxue
         * @Date 18:29 2018/9/29
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/saveGift",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> saveGift(@RequestBody Map<String,Object> param){
        Map<String,Object> ResultMap = new HashMap<>();
        ResultMap = projectService.saveGift(param);
        return ResultMap;
    }

    /**
         * @Description 删除礼品信息
         * @Author zhangxue
         * @Date 18:50 2018/9/29
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/delGiftInfo",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> delGiftInfo(@RequestBody Map<String,Object> param,HttpServletRequest request){
        Map<String,Object> ResultMap = new HashMap<>();
        HttpSession session = request.getSession();
        String SessionUserNo = session.getAttribute("UserNo").toString();
        String UserNo = param.get("UserNo").toString();
        if(SessionUserNo.equals(UserNo)){
            ResultMap = projectService.delGiftInfo(param);
            return ResultMap;
        }else {
            ResultMap.put("Status",-1);
            return ResultMap;
        }

    }

    /**
         * @Description 查询所有礼品
         * @Author zhangxue
         * @Date 18:36 2018/9/29
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/queryAllGifts",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> queryAllGifts(@RequestBody Map<String,Object> param){
        Map<String,Object> ResultMap = new HashMap<>();
        List<Map<String,Object>> ResultList = projectService.queryAllGifts(param);
        ResultMap.put("ResultList",ResultList );
        return ResultMap;
    }

    /**
         * @Description 通过类型查询总金额
         * @Author zhangxue
         * @Date 9:43 2018/10/1
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/queryMoneyByType",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> queryMoneyByType(@RequestBody Map<String,Object> param){
        Map<String,Object> ResultMap = new HashMap<>();
        List<Map<String,Object>> ResultList = projectService.queryMoneyByType(param);
        ResultMap.put("ResultList",ResultList );
        Integer TypeMoney = 0;
        for(int i=0;i<ResultList.size();i++){
            Integer TempMoney = Integer.valueOf(ResultList.get(i).get("TotalMoney").toString());
            TypeMoney += TempMoney;
        }
        ResultMap.put("TypeMoney",TypeMoney);
        return ResultMap;
    }

    /**
         * @Description 查询所有店面详细数据
         * @Author zhangxue
         * @Date 10:16 2018/10/4
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/queryStoreDataInfo",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> queryStoreDataInfo(@RequestBody Map<String,Object> param){
        List<Map<String,Object>> ResultList = projectService.queryStoreDataInfo(param);
        Map<String,Object> ResultMap = new HashMap<String, Object>();
        ResultMap.put("ResultList",ResultList);
        return ResultMap;
    }

    /**
         * @Description 录入销售总额或销售任务
         * @Author zhangxue
         * @Date 9:14 2018/11/9
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/saveDataTotal",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> saveDataTotal(@RequestBody Map<String,Object> param){
        Map<String,Object> ResultMap = new HashMap<>();
        ResultMap = projectService.saveDataTotal(param);
        return ResultMap;
    }

    /**
         * @Description 查询销售任务和销售总额
         * @Author zhangxue
         * @Date 9:51 2018/11/9
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/queryDataTotal",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> queryDataTotal(@RequestBody Map<String,Object> param){
        Map<String,Object> ResultMap = new HashMap<>();
        List<Map<String,Object>> ResultList = projectService.queryDataTotal(param);
        ResultMap.put("ResultList",ResultList );
        int SaleTotalSum = 0;
        int TaskTotalSum = 0;
        for(int i=0;i<ResultList.size();i++){
            int SaleTotal = Integer.valueOf(ResultList.get(i).get("SaleTotal").toString());
            int TaskTotal = Integer.valueOf(ResultList.get(i).get("TaskTotal").toString());
            SaleTotalSum += SaleTotal;
            TaskTotalSum += TaskTotal;

        }
        Double SaleTotalSumD = Double.valueOf(SaleTotalSum);
        Double TaskTotalSumD = Double.valueOf(TaskTotalSum);
        Double FinshRate = SaleTotalSumD/TaskTotalSumD;
        ResultMap.put("SaleTotalSum",SaleTotalSum);
        ResultMap.put("TaskTotalSum",TaskTotalSum);
        FinshRate = FinshRate*100;
        String FinshRateStr = String.format("%.2f", FinshRate);
        ResultMap.put("FinshRate",FinshRateStr);

        return ResultMap;
    }
}
